package controller

import (
	"errors"
	"fmt"
	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
	"tino_manager/dao"
	"tino_manager/dto"
	"tino_manager/middleware"
)

type SwiperController struct {
}

func SwiperRegister(group *gin.RouterGroup) {
	swiper := &SwiperController{}
	//创建通知
	group.POST("/add", swiper.Add)
	//通知列表
	group.GET("/page", swiper.Page)
	//获取通知详情
	group.GET("/detail", swiper.Detail)
	//修改通知详情
	group.POST("/update", swiper.Update)
	//删除公告
	group.POST("/delete", swiper.Delete)
}

func (n *SwiperController) Add(c *gin.Context) {
	params := &dto.SwiperAddInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	swiper := &dao.Swiper{
		LinkType: params.LinkType,
		LinkId:   params.LinkId,
		Sort:     params.Sort,
		Image:    params.Image,
	}
	swiper.InitData(tx)
	if err := swiper.Save(c, tx); err != nil {
		fmt.Println(err)
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
func (n *SwiperController) Page(c *gin.Context) {
	params := &dto.SwiperPageInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	total := int64(0)
	offset := (params.PageNumber - 1) * params.PageSize
	var list []dao.Swiper
	query := tx.WithContext(c).Model(&dao.Swiper{}).Where("is_delete=0").Order("sort desc")
	query.Count(&total)
	if err := query.Limit(params.PageSize).Offset(offset).Find(&list).Error; err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败"), true)
		return
	}
	out := &dto.SwiperPageOutput{
		Total: total,
	}
	for _, item := range list {
		out.List = append(out.List, dto.SwiperInfo{
			Id:        fmt.Sprintf("%d", item.Id),
			LinkType:  item.LinkType,
			LinkId:    item.LinkId,
			Image:     item.Image,
			Status:    item.Status,
			Sort:      item.Sort,
			CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	middleware.ResponseSuccess(c, out, false)
}
func (n *SwiperController) Detail(c *gin.Context) {
	params := &dto.SwiperDetailInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败"), true)
		return
	}
	swiper := &dao.Swiper{}
	if result := tx.Where(map[string]interface{}{
		"id": params.Id,
	}).First(swiper); result.RowsAffected == 0 {
		middleware.ResponseError(c, 500, errors.New("获取详情失败"), true)
		return
	}
	middleware.ResponseSuccess(c, dto.SwiperInfo{
		Id:        fmt.Sprintf("%d", swiper.Id),
		Sort:      swiper.Sort,
		Image:     swiper.Image,
		Status:    swiper.Status,
		LinkType:  swiper.LinkType,
		LinkId:    swiper.LinkId,
		CreatedAt: swiper.CreatedAt.Format("2006-01-02 15:04:05"),
	}, false)
}
func (n *SwiperController) Update(c *gin.Context) {
	params := &dto.SwiperUpdateInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	swiper := &dao.Swiper{
		Image:    params.Image,
		LinkId:   params.LinkId,
		LinkType: params.LinkType,
		Sort:     params.Sort,
	}
	if result := tx.Model(&dao.Swiper{}).Where("id = ?", params.Id).Select("link_type", "image", "link_id", "sort").Updates(swiper); result.Error != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
func (n *SwiperController) Delete(c *gin.Context) {
	params := &dto.SwiperDeleteInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	if result := tx.Model(&dao.Swiper{}).Where("id = ?", params.Id).Update("is_delete", 1); result.Error != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
